<?php

class Notification{
	
	public static function send($fromUserId, $fromUserName, $receiverIds, $receiverNames, $title, $content){
		$ids = explode(";", $receiverIds);
		$names = explode(";", $receiverNames);
		
		for ($i= 0; $i < count($ids) - 1; $i++){
			$notification = new NotificationLine(array(
				'fromUserId'	=> $fromUserId,
				'fromUserName'	=> $fromUserName,
				'toUserId'		=> $ids[$i],
				'toUserName'	=> $names[$i],
				'title'	        => $title,
				'content'	    => $content
			));
            $insertID = $notification -> save();
        }
	
		return array(
			'status'	=> 1,
			'insertID'  => $insertID,
			'ids' => $ids,
			'fromUserId'	=> $fromUserId,
				'fromUserName'	=> $fromUserName,

				'toUserName'	=> $names,
				'title'	        => $title,
				'content'	    => $content
			
		);
	}
	
	public static function delete($id){
		$id = (int)$id;
		$result = DB::query('DELETE FROM notifications WHERE id = '.$id);
		return array('result' => $result);
	}
	
	public static function read($id){
		$id = (int)$id;
		$result = DB::query('UPDATE notifications set is_read=1 WHERE id = '.$id);
		return array('result' => $result);
	}
	
	public static function getReceived($userId){
		$userId = (int)$userId;
	    $received = array();
		$result = DB::query('SELECT * FROM notifications WHERE to_user_id = '.$userId.' ORDER BY ts ASC');
	
		
		while($item = $result->fetch_object()){
			
			// Returning the GMT (UTC) time of the chat creation:
			/*
			$item->time = array(
				'hours'		=> gmdate('H',strtotime($item->ts)),
				'minutes'	=> gmdate('i',strtotime($item->ts))
			);
			*/
			$received[] = $item;
		}
		return array('result' => $received);
	}
	
	public static function getSent($userId){
		$userId = (int)$userId;
	
		$result = DB::query('SELECT * FROM notifications WHERE from_user_id = '.$userId.' ORDER BY ts ASC');
	
		$received = array();
		while($item = $result->fetch_object()){
			
			// Returning the GMT (UTC) time of the chat creation:
			/*
			$item->time = array(
				'hours'		=> gmdate('H',strtotime($item->ts)),
				'minutes'	=> gmdate('i',strtotime($item->ts))
			);
			*/
			$received[] = $item;
		}
		return array('result' => $received);
	}
}


?>